(function ($) {
$.fn.extend({
    //插件名称
    mytab: function (options) {

        //参数和默认值
        var defaults = {
			currentNavAttrName:"tab-nav-current",
        	tabNavDivClass: "ui-tab-nav",
			currentTabNavClass:"ui-tab-nav-current",
            hideAfterInit:true, //暂时未启用 20140908
			skinName:"default",
			
			tabContainerDivId:"tab-container",
			tabNavDivId:"tabNavDiv"
			
        };

        var options = $.extend(defaults, options);


        return this.each(function () {
			var tabContainerDivId = "#" + options.tabContainerDivId;
			var tabNavDivId = "#" + options.tabNavDivId;
			
			var mytab = {

                tabNavStatChange:function ($tabNavLi){
                    var currentNavAttrName = options.currentNavAttrName;
                    $($tabNavLi).siblings().attr(currentNavAttrName, "").removeClass(options.currentTabNavClass);
                    $($tabNavLi).attr(currentNavAttrName ,"current").addClass(options.currentTabNavClass);
                },

                tabStatChange:function ($tabNavLi) {
                    var showDivId = $($tabNavLi).attr("targetDivId");
                    $($tabNavLi).siblings().each(function() {
                        var tabContentId = $(this).attr("targetDivId");;
                        $(tabContainerDivId + " #"+tabContentId).hide();
                    });
                    $(tabContainerDivId+" div[id=" + showDivId + "]").show();
                },

				init : function () {
					var liSelector = tabContainerDivId + " "+tabNavDivId+" ul li";
					//alert(liSelector);
					$(tabContainerDivId).addClass(options.skinName);
					$(tabNavDivId).addClass(options.tabNavDivClass);
					//$(liSelector).addClass(options.tabNavDivClass);
					$(liSelector).each(function(i){
						$(this).click(function() {
                            mytab.tabNavStatChange($(this));
                            mytab.tabStatChange($(this));
						});
					});

					var currentNav;
                    var currentNavAttrName = options.currentNavAttrName;
					$(liSelector).each(function () {
                        if ($(this).attr(currentNavAttrName) == "current") {
                            currentNav = $(this)
                        }
					});

                    if(!currentNav) {
                        currentNav = $(liSelector).first();
                    }
                    currentNav.trigger("click");

				}
			};
			
			mytab.init();
        });
    }
    });})(jQuery);
